package com.bnd.utils;

import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.bnd.domain.ChatHistory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Message和ChatHistory转换工具类
 */
public class MessageChatHistoryConverter {

    /**
     * 将ChatHistory转换为Message
     * 
     * @param chatHistory 聊天历史记录
     * @return Message对象
     */
    public static Message chatHistoryToMessage(ChatHistory chatHistory) {
        if (chatHistory == null) {
            return null;
        }
        
        String roleValue;
        switch (chatHistory.getRole()) {
            case "0":
                roleValue = Role.USER.getValue();
                break;
            case "1":
                roleValue = Role.ASSISTANT.getValue();
                break;
            case "2":
                roleValue = Role.SYSTEM.getValue();
                break;
            default:
                roleValue = Role.USER.getValue();
        }
        
        return Message.builder()
                .role(roleValue)
                .content(chatHistory.getContent())
                .build();
    }

    /**
     * 将Message转换为ChatHistory
     * 
     * @param message 消息对象
     * @param chatId 会话ID
     * @param characterId 角色ID
     * @return ChatHistory对象
     */
    public static ChatHistory messageToChatHistory(Message message, String chatId, Long characterId) {
        if (message == null) {
            return null;
        }
        
        ChatHistory chatHistory = new ChatHistory();
        chatHistory.setChatId(chatId);
        chatHistory.setContent(message.getContent());
        chatHistory.setCharacterId(characterId);
        
        if (Role.USER.getValue().equals(message.getRole())) {
            chatHistory.setRole("0");
        } else if (Role.SYSTEM.getValue().equals(message.getRole())) {
            chatHistory.setRole("2");
        } else {
            // 助手
            chatHistory.setRole("1");
        }
        
        return chatHistory;
    }

    /**
     * 批量将ChatHistory列表转换为Message列表
     * 
     * @param chatHistoryList 聊天历史记录列表
     * @return Message对象列表
     */
    public static List<Message> chatHistoryListToMessageList(List<ChatHistory> chatHistoryList) {
        if (chatHistoryList == null || chatHistoryList.isEmpty()) {
            return List.of();
        }
        
        return chatHistoryList.stream()
                .map(MessageChatHistoryConverter::chatHistoryToMessage)
                .collect(Collectors.toList());
    }

    /**
     * 批量将Message列表转换为ChatHistory列表
     * 
     * @param messageList 消息对象列表
     * @param chatId 会话ID
     * @param characterId 角色ID
     * @return ChatHistory对象列表
     */
    public static List<ChatHistory> messageListToChatHistoryList(List<Message> messageList, String chatId, Long characterId) {
        if (messageList == null || messageList.isEmpty()) {
            return List.of();
        }
        
        return messageList.stream()
                .map(message -> messageToChatHistory(message, chatId, characterId))
                .collect(Collectors.toList());
    }
}
